package com.fa.wisdom.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.fa.wisdom.utils.ViewClickListen;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Steven on 2019/1/4.
 */
public abstract class BaseFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = setContentView(inflater);
        return view;
    }

    private CompositeSubscription compositeSubscription;

    private CompositeSubscription getCompositeSubscription() {
        if (compositeSubscription == null) {
            compositeSubscription = new CompositeSubscription();
        }
        return compositeSubscription;
    }

    public void addClick(View v, final ViewClickListen listen) {
        getCompositeSubscription().add(RxView.clicks(v)
                .throttleFirst(500, TimeUnit.MILLISECONDS)//限制 500毫秒内
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        listen.call();
                    }
                }));
    }

    private void unRegisterSubscription() {
        if (compositeSubscription != null) {
            compositeSubscription.clear();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unRegisterSubscription();
    }


    public abstract View setContentView(LayoutInflater inflater);

}
